<template>
  <div>
    <dialog-edit :visible.sync="dialogVisible" :title="$attrs.Title||'新增任务'" ref="userManageEdit" width="400"
                 height="200" @save="save" :saveLoading="saveLoading">
      <template slot="body">
        <el-form ref="form" :model="preFormData" label-width="80px" :rules="rules" style="margin: 30px 0">
          <el-form-item label="建模名称" prop="name">
            <el-input v-model="preFormData.name"></el-input>
          </el-form-item>
        </el-form>
      </template>
    </dialog-edit>
  </div>
</template>

<script>
import dialogEdit from '@/components/element/dialogEdit'
import dymForm from '@/components/element/dymForm'
import testDialog from '@/page/ProjectManage/modules/proTest/testDialog'
import projectDocApi from '@/api/projectDocApi'

export default {
  name: "modelDialog",
  components: {
    dialogEdit,
    dymForm,
    testDialog
  },
  props: {
    visible: {
      type: Boolean,
      default() {
        return false;
      }
    },
    formData: {
      type: Object,
      default() {
        return {};
      }
    }
  },
  data () {
    return {
      saveLoading: false,
      testDialogVisible: false,
      preFormData: {
        name: '建模名称'
      },
      rules: {
        name: {required: true, message: '名称必填且长度不超过15', max: 15, trigger: 'change'}
      }
    }
  },
  computed: {
    dialogVisible: {
      get: function () {
        return this.visible
      },
      set: function (value) {
        this.$emit('update:visible', value)
      }
    }
  },
  methods: {
    save () {
      this.$refs.form.validate().then(res => {
        let c = {
          Projectid: this.formData.Projectid,
          Title: this.preFormData.name
        }
        this.saveLoading = true
        projectDocApi.create(c).then(res => {
          this.dialogVisible = false
          if (res && res.code === 1) {
            this.$router.push(`/view/project/modelingIndex/${res.id}`)
          } else {
            this.error('创建失败')
          }
        }).finally(() => {
          this.saveLoading = false
        })
      }).catch(res => {
        this.warning('查看标红位置是否正确')
      })
    }
  }
}
</script>

<style scoped>
.dymForm >>> .el-date-editor {
  width: 100%;
}
</style>